//
//  WXPDeferRepaymentWayView.m
//  MexicoLoan
//
//  Created by wxp on 2025/3/19.
//

#import "WXPDeferRepaymentWayView.h"

@implementation WXPDeferRepaymentWayView

#pragma mark - lazy
- (UIView *)viewBack{
    if (!_viewBack) {
        _viewBack = [UIView new];
        _viewBack.width = SCREEN_WIDTH-LEFT_MARDIN*2;
        _viewBack.backgroundColor = COLOR_WHITE;
        _viewBack.userInteractionEnabled = true;
        [GlobalMethod setRoundView:_viewBack color:[UIColor clearColor] numRound:16 width:0];
    }
    return _viewBack;
}
- (UILabel *)titleLabel
{
    if (_titleLabel == nil) {
        _titleLabel = [UILabel  new];
        _titleLabel.textColor = COLOR_BLACK;
        _titleLabel.font = [UIFont boldSystemFontOfSize:(16)];
        [_titleLabel fitTitle:@"Elección del método de pago：" variable:0];
    }
    return  _titleLabel;
}
- (UIView *)btnBackView{
    if (_btnBackView == nil) {
        _btnBackView = [UIView new];
    }
    return _btnBackView;
}
- (UILabel *)contentLabel{
    if (_contentLabel == nil) {
        _contentLabel = [UILabel new];
        _contentLabel.textColor = COLOR_CONTENT;
        _contentLabel.fontNum = (12);
        _contentLabel.numberOfLines = 0;
        _contentLabel.lineSpace = (2);
    }
    return _contentLabel;
}
- (WXPPaymenCodeView *)paymentCodeView
{
    if (_paymentCodeView == nil) {
        _paymentCodeView = [WXPPaymenCodeView  new];
    }
    return  _paymentCodeView;
}
- (UILabel *)htmlTitleLabel
{
    if (_htmlTitleLabel == nil) {
        _htmlTitleLabel = [UILabel  new];
        _htmlTitleLabel.textColor = COLOR_BLACK;
        _htmlTitleLabel.font = [UIFont boldSystemFontOfSize:(16)];
    }
    return  _htmlTitleLabel;
}
- (UILabel *)htmlLabel
{
    if (_htmlLabel == nil) {
        _htmlLabel = [UILabel  new];
        _htmlLabel.numberOfLines = 0;
    }
    return  _htmlLabel;
}
- (WXPBarCodeView *)barCodeView
{
    if (_barCodeView == nil) {
        _barCodeView = [WXPBarCodeView  new];
    }
    return  _barCodeView;
}

#pragma mark - 初始化
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.width = SCREEN_WIDTH;
        [self addSubView];
    }
    return self;
}

//添加subview
- (void)addSubView{
    [self addSubview:self.viewBack];
    [self.viewBack addSubview:self.titleLabel];
    [self.viewBack addSubview:self.btnBackView];
    [self.viewBack addSubview:self.contentLabel];
    [self.viewBack addSubview:self.paymentCodeView];
    [self.viewBack addSubview:self.barCodeView];
    [self addSubview:self.htmlTitleLabel];
    [self addSubview:self.htmlLabel];
    //初始化页面
    [self resetPaymenyWayViewWithAry:nil];
}

#pragma mark - 点击事件
- (void)wayItemBtnClick:(UIButton *)sender{
    
    for (UIButton * btn in self.btnBackView.subviews) {
        btn.selected = false;
    }
    sender.selected = true;
    self.selectModel = self.aryDatas[sender.tag];
    [self resetPaymentCodeBackView];
    [self resetContentLabel];
    if(self.blockResetHeaderView){
        self.blockResetHeaderView(self.selectModel);
    }
}

#pragma mark - 刷新view
- (void)resetPaymenyWayViewWithAry:(NSArray *)array{
    self.aryDatas = array;
    
    //刷新view
    self.titleLabel.leftTop = XY(LEFT_MARDIN, 20);
    
    self.btnBackView.widthHeight = XY((self.viewBack.width-2*LEFT_MARDIN), (self.viewBack.width-2*LEFT_MARDIN-20)/2/146*44);
    [self.btnBackView removeAllSubViews];
    CGFloat btnWidth = (self.btnBackView.width-(20))/2;
    if(isAry(array)){
        for (int i = 0; i < array.count; i ++) {
            UIButton * btn = [UIButton new];
            ModelRepaymentCodeList * model = array[i];
            btn.widthHeight = XY(btnWidth, btnWidth/146*44);
            btn.leftTop = XY((btnWidth+(20))*i, 0);
            [btn setImage:[UIImage imageNamed:model.imageName] forState:UIControlStateNormal];
            [btn setImage:[UIImage imageNamed:model.highImageName] forState:UIControlStateSelected];
            self.btnBackView.height = btn.bottom;
            [self.btnBackView addSubview:btn];
            btn.selected = model.isSelected;
            if(model.isSelected){
                self.selectModel = model;
            }
            btn.tag = i;
            [btn addTarget:self action:@selector(wayItemBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        }
    }
    self.btnBackView.centerXTop = XY(self.viewBack.width/2, self.titleLabel.bottom+(16));
    // 固定文本显示
    [self resetContentLabel];
    
    // 付款码
    [self resetPaymentCodeBackView];
}

// 重置付款码
- (void)resetPaymentCodeBackView{
    self.barCodeView.hidden = self.selectModel.tag == 1;
    if(self.barCodeView.hidden == false){
        self.barCodeView.hidden = !isStr(self.selectModel.barcodeUrlAlfa);
    }
    if(self.selectModel.tag != 1){
        [self.barCodeView resetViewWithModel:self.selectModel];
        self.barCodeView.centerXTop = XY(SCREEN_WIDTH/2, self.contentLabel.bottom+(22));
    }
    [self.paymentCodeView resetViewWithModel: self.selectModel];
    if(self.barCodeView.hidden){
        self.paymentCodeView.leftTop = XY(0, self.contentLabel.bottom+(22));
    }else{
        self.paymentCodeView.leftTop = XY(0, self.barCodeView.bottom+(22));
    }

    self.viewBack.height = self.paymentCodeView.bottom+LEFT_MARDIN;
    self.viewBack.centerXTop = XY(self.width/2, 0);
    
    [self.htmlTitleLabel fitTitle:self.selectModel.tag==1?@"Como realizar el pago":self.selectModel.tag==2?@"Openpay:":@"oxxo:" variable:0];
    self.htmlTitleLabel.leftTop = XY((20), self.viewBack.bottom+(20));
    
    // 加载包含HTML标签的字符串
    NSString *htmlString = self.selectModel.repaymentLead;
    // 将HTML字符串转换为NSAttributedString
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
    // 设置字体颜色
    [attributedString addAttribute:NSForegroundColorAttributeName value:COLOR_BLACK range:NSMakeRange(0, attributedString.length)];
    // 设置字体大小
    [attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:(14)] range:NSMakeRange(0, attributedString.length)];
    // 设置行间距
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = (2);
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, attributedString.length)];
    // 设置UILabel的属性文本
    self.htmlLabel.width = SCREEN_WIDTH-2*(20);
    self.htmlLabel.attributedText = attributedString;
    [self.htmlLabel sizeToFit];
    self.htmlLabel.leftTop = XY((20), self.htmlTitleLabel.bottom+(8));
    
    self.height = self.htmlLabel.bottom+LEFT_MARDIN;
}

- (void)resetContentLabel{ 
    NSString * bancosStr = @"Realice el pago a través de transferencia bancaria a la cuenta bancaria designada.";
    NSString * paynetStr = @"Puede realizar el pago en efectivo en las tiendas asociadas o a través de transferencia bancaria en línea.";
    NSString * oxxoStr = @"Por favor, especifique el número de referencia del cajero en esta etiqueta para que pueda ingresarlo directamente en la pantalla de ventas";
    
    [self.contentLabel fitTitle:(self.selectModel.tag==1)?bancosStr:(self.selectModel.tag==2)?paynetStr:oxxoStr variable:self.viewBack.width-2*LEFT_MARDIN];
    self.contentLabel.leftTop = XY(LEFT_MARDIN, self.btnBackView.bottom+LEFT_MARDIN);
}

@end
